package com.zs.usc.controller;


import com.zs.usc.common.BaseAuthController;
import com.zs.usc.common.RestRst;
import com.zs.usc.common.log.SysMethodLog;
import com.zs.usc.common.log.SysModuleLog;
import com.zs.usc.common.log.enums.LogTypes;
import com.zs.usc.common.log.enums.OperationTypes;
import com.zs.usc.common.validate.MyValidate;
import com.zs.usc.vo.LoginInfo;
import com.zs.usc.entity.UserInfo;
import com.zs.usc.service.IUserService;
import com.zs.usc.utils.ReqUtils;
import com.zs.usc.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zs
 * @since 2021-11-11
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户中心", value = "用户中心")
@SysModuleLog(name = "用户中心")
public class UserController extends BaseAuthController {


    @Autowired
    private IUserService userService;


    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    @SysMethodLog(logTypes = LogTypes.LOGIN, operationType = OperationTypes.LOGIN)
    public RestRst<UserInfoVO> login(@RequestBody LoginInfo loginInfo, HttpServletRequest request) {
        String ip = ReqUtils.getIp(request);
        return RestRst.buildSuccess(userService.simpleLogin(loginInfo.getUsername(), loginInfo.getPassword(), ip));
    }

    @PostMapping("/saveUser")
    @ApiOperation(value = "注册用户")
    @MyValidate
    public RestRst<Void> saveUser(@RequestBody  UserInfo userInfo) {
        userService.saveUser(userInfo);
        return RestRst.buildSuccess(null);
    }

}

